package com.craftcomputing.client.Entry;

import com.craftcomputing.client.Entry.PhotoWidget.PhotoWidget;
import com.craftcomputing.client.Listeners.EntryPanelListeners;
import com.craftcomputing.client.Listeners.SearchResultLabelListeners.ImageLoadListener;
import com.craftcomputing.client.Listeners.SearchResultListeners.JSONLinkedEntryResponseTextHandler;
import com.craftcomputing.client.SearchResult.CodeSearchResult;
import com.craftcomputing.client.SearchResult.MaterialSearchResult;
import com.craftcomputing.client.SearchResult.ThemeSearchResult;
import com.craftcomputing.client.SearchResult.WorkshopSearchResult;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.Window;

public class WorkshopEntryPanel extends Composite {

	public WorkshopEntryPanel() {
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		panel.setStyleName("entryPanel");
		
		final VerticalPanel verticalPanel_4 = new VerticalPanel();
		panel.add(verticalPanel_4);
		verticalPanel_4.setWidth("100%");	

//		verticalPanel_4.add(titleLabel);
//		titleLabel.setHeight("25px");
//		titleLabel.setStyleName("gwt-Label2");

		final Label headerLabel = new Label("more workshop details");
		headerLabel.setStyleName("moreDetails");
		moreResults.setHeader(headerLabel);
		panel.add(moreResults);
		moreResults.setWidth("792px");
		moreResults.setStyleName("entryPanel");
		moreResults.setOpen(false);
		moreResults.setVisible(true);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		moreResults.setContent(horizontalPanel);
		horizontalPanel.setWidth("800px");
		horizontalPanel.setSpacing(4);
		
		final HTML html = new HTML("more results panel blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
		horizontalPanel.add(html);

		final VerticalPanel verticalPanel_2 = new VerticalPanel();
		panel.add(verticalPanel_2);
		verticalPanel_2.setSize("100%", "100%");

		verticalPanel_2.add(tabPanel);

		tabPanel.add(materialsPanel, "Materials");
		materialsPanel.setSize("796px", "100%");
		materialsPanel.setStyleName("gwt-TabPanel-Results");

		materialsPanel.add(materialSearchResult);
		materialSearchResult.setSize("100%", "100%");

		tabPanel.add(themesPanel, "Themes");
		themesPanel.setSize("796px", "100%");
		themesPanel.setStyleName("gwt-TabPanel-Results");

		themesPanel.add(themeSearchResult);
		themeSearchResult.setSize("100%", "100%");

		tabPanel.add(codePanel, "Code");
		codePanel.setSize("796px", "100%");
		codePanel.setStyleName("gwt-TabPanel-Results");

		codePanel.add(codeSearchResult);
		codeSearchResult.setSize("100%", "100%");
		tabPanel.selectTab(0);

		verticalPanel_4.add(horizontalPanel_4);
		horizontalPanel_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel_4.add(photo);
		horizontalPanel_4.setCellHorizontalAlignment(photo, HasHorizontalAlignment.ALIGN_LEFT);
//		photo.setUrl("lightbox/close.gif");
//		photoClick = new PhotoAlbumClickListener(photo.getUrl());

		horizontalPanel_4.add(richTextArea);
		horizontalPanel_4.setCellWidth(richTextArea, "100%");
		horizontalPanel_4.setCellHorizontalAlignment(richTextArea, HasHorizontalAlignment.ALIGN_LEFT);
		//richTextArea.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		richTextArea.setWidth("455px");
		richTextArea.setStyleName("gwt-RichTextArea");
		

	}
	public void update(JSONValue json){
    	final String id = json.isObject().get("recordId").toString().replaceAll("\"", "");
    	final String workshopName = json.isObject().get("title").toString().replaceAll("\"", "");
    	final String imageLoc = json.isObject().get("photoFolder").toString().replaceAll("\"", "");
    	final String sdesc = json.isObject().get("shortDesc").toString().replaceAll("\"", "");
    	final String ldesc = json.isObject().get("longDesc").toString().replaceAll("\"", "");
    	final String insp = json.isObject().get("insp").toString().replaceAll("\"", "");
    	final String photos = json.isObject().get("photos").toString().replaceAll("\"", "");
    	final String date = json.isObject().get("date").toString().replaceAll("\"", "");
    	final String facs = json.isObject().get("facilitators").toString().replaceAll("\"", "");
    	final String loc = json.isObject().get("location").toString().replaceAll("\"", "");
		titleLabel.setText(workshopName);

		horizontalPanel_4.remove(photo);
		//photo = new PhotoWidget(imageLoc);
		photo = new PhotoWidget(imageLoc + ", " + photos);
		horizontalPanel_4.insert(photo, 0);
		String html = "";

		if(!date.equals(""))
			html = html + "held on " + date;
			if(!facs.equals(""))
				html = html + " at " + facs + "<br><br>";
			else
				html = html + "<br><br>";			
		if(!sdesc.equals(""))
			html = html + sdesc;
		richTextArea.setHTML(html);
		
		if(json.isObject().get("codeSamples") != null){
			JSONArray codesamples = json.isObject().get("codeSamples").isArray();
			if(codesamples.size() > 0)
				codeSearchResult.setLabels(codesamples, "");
			else
				tabPanel.remove(codePanel);
		}else{
			String searchText = "/python/workshop/codesamples?workshopId=" + id;
			HTTPRequest.asyncGet(searchText, new JSONLinkedEntryResponseTextHandler(codeSearchResult));						
		}
		
		if(json.isObject().get("themes") != null){
			JSONArray themes = json.isObject().get("themes").isArray();
			if(themes.size() > 0)
				themeSearchResult.setLabels(themes, "");
			else
				tabPanel.remove(themesPanel);
		}else{
			String searchText = "/python/workshop/themes?workshopId=" + id;
			HTTPRequest.asyncGet(searchText, new JSONLinkedEntryResponseTextHandler(themeSearchResult));			
		}

		if(json.isObject().get("materials") != null){
			JSONArray materials = json.isObject().get("materials").isArray();
			if(materials.size() > 0)
				materialSearchResult.setLabels(materials, "");
			else
				tabPanel.remove(materialsPanel);
		}else{
			String searchText = "/python/workshop/materials?workshopId=" + id;
			HTTPRequest.asyncGet(searchText, new JSONLinkedEntryResponseTextHandler(materialSearchResult));						
		}
			
		if(ldesc.equals("") | insp.equals(""))
			moreResults.setVisible(false);
		else{
			//Window.alert("insp: " + Integer.toString(insp.length()) + "\ndesc: " + Integer.toString(ldesc.length()));
			html = "";
			if(!ldesc.equals(""))
				html = html + ldesc.replace('\u000B', '\n') + "<br><br>";
			if(!insp.equals(""))
				html = html + insp.replace('\u000B', '\n');
			final HTML html2 = new HTML(html);
			//html2.setWidth("100%");
			final HorizontalPanel horizontalPanel = new HorizontalPanel();
			horizontalPanel.setWidth("792px");
			horizontalPanel.setSpacing(4);
			horizontalPanel.add(html2);
			
			moreResults.setContent(horizontalPanel);
			moreResults.setVisible(true);
			moreResults.setStyleName("entryPanel");
		}
		tabPanel.selectTab(0);
	}
	//private Image photo  = new Image("home.gif");	
	private PhotoWidget photo  = new PhotoWidget("lightbox/close.gif");	
	private Label titleLabel = new Label("Workshop Title");
	private HTML richTextArea = new HTML();
	private DisclosurePanel moreResults = new DisclosurePanel("more results");
	//private RichTextArea richTextArea = new RichTextArea();
	private ThemeSearchResult themeSearchResult = new ThemeSearchResult(false);
	private MaterialSearchResult materialSearchResult = new MaterialSearchResult(false);
	private CodeSearchResult codeSearchResult = new CodeSearchResult(false);
	private VerticalPanel materialsPanel = new VerticalPanel();
	private VerticalPanel themesPanel = new VerticalPanel();
	private VerticalPanel codePanel = new VerticalPanel();
	private TabPanel tabPanel = new TabPanel();
	private HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
	public String getTitle(){
		return titleLabel.getText();
	}
}
